package com.wisezone.studentmanger.servlet;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

public class DBTest {

	public static void main(String[] args) {

		// 1.加载数据库驱动
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		// 2.建立连接
		Connection connection = null;
		try {
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentsystemdb", "root", "123");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		// 3.操作数据
		String sql = "SELECT * FROM student";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = connection.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				int number = rs.getInt("number");
				String name = rs.getString("name");
				Timestamp time = rs.getTimestamp("datetime");
				
				System.out.println("学号:"+number+" 姓名:"+name+" 时间:"+time);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 4.关闭数据连接
			try {
				if (rs != null)
					rs.close();
				if (ps != null)
					ps.close();
				if (connection != null)
					connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}


	}

}
